using DimensionsHelper.LanguageService.Symbols;
using DimensionsHelper.LanguageService.Syntax;

namespace DimensionsHelper.LanguageService;

public class AnalysisForEachStatement : AnalysisStatement
{
    internal AnalysisForEachStatement(ForEachStatement syntaxNode,
        AnalysisIdentifier iterator,
        AnalysisExpression group,
        AnalysisBlock body,
        AnalysisNode? parent) : base(
        AnalysisBoundKinds.ForEachStatement, syntaxNode, parent)
    {
        Syntax = syntaxNode;

        Iterator = iterator;
        Group = group;
        Body = body;

        Iterator.Parent = this;
        Group.Parent = this;
        Body.Parent = this;
    }


    public override ForEachStatement Syntax { get; }

    public AnalysisIdentifier Iterator { get; }

    public AnalysisExpression Group { get; }

    public AnalysisBlock Body { get; }


    public override int ChildrenCount => 3;

    public override IEnumerable<AnalysisNode> GetChildren()
    {
        return [Iterator, Group, Body];
    }

    internal override void AddChild(AnalysisNode child)
    {
        throw new NotSupportedException("AnalysisForEachStatement is read-only.");
    }
}

public static partial class AnalysisNodeFactory
{
    /// <summary>
    /// 创建一个For Each循环语句
    /// </summary>
    /// <param name="syntaxNode">原始语法节点</param>
    /// <param name="iterator">迭代器变量</param>
    /// <param name="group">集合表达式</param>
    /// <param name="body">循环语句体</param>
    /// <param name="parent">父节点，可以忽略</param>
    /// <param name="withSymbol">语法节点所在的With语句上下文类型符号</param>
    public static AnalysisForEachStatement ForEachStatement(ForEachStatement syntaxNode,
        AnalysisIdentifier iterator,
        AnalysisExpression group,
        AnalysisBlock body,
        AnalysisNode? parent,
        ITypeSymbol? withSymbol)
    {
        return new AnalysisForEachStatement(syntaxNode, iterator, group, body, parent)
            .WithContainingWithSymbol(withSymbol);
    }
}